package com.codeczx.customviewdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by win10 on 2017/6/28.
 */

public class EasyRelativeLayout extends ViewGroup {

    private static final String TAG = "EasyRelativeLayout";

    public EasyRelativeLayout(Context context) {
        super(context);
    }

    public EasyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EasyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMdoe = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);


        int layoutWidth = 0;
        int layoutHeight = 0;

        int cWidth;
        int cHeight;

        for (int i = 0; i < getChildCount(); i++) {
            measureChildWithMargins(getChildAt(i), widthMeasureSpec, 0, heightMeasureSpec, 0);
        }
//        measureChildren(widthMeasureSpec, heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            layoutWidth = widthSize;
        } else {
            EasyRelativeLayoutParams lp = null;
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                cWidth = child.getMeasuredWidth();
                int marginWidth = 0;
                lp = (EasyRelativeLayoutParams) child.getLayoutParams();
                marginWidth = cWidth + lp.leftMargin + lp.rightMargin;
                layoutWidth = marginWidth > layoutWidth ? marginWidth : layoutWidth;
//                layoutWidth = cWidth > layoutWidth ? cWidth : layoutWidth;

            }
        }

        if (heightMdoe == MeasureSpec.EXACTLY) {
            layoutHeight = heightSize;
        } else {
            EasyRelativeLayoutParams lp = null;
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                cHeight = child.getMeasuredHeight();
                lp = (EasyRelativeLayoutParams) child.getLayoutParams();
                int marginHeight = cHeight + lp.topMargin + lp.bottomMargin;
                layoutHeight = marginHeight > layoutHeight ? marginHeight : layoutHeight;
            }
        }
        setMeasuredDimension(layoutWidth, layoutHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childMeasureWidth = 0;
        int childMeasureHeight = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            childMeasureWidth = child.getMeasuredWidth();
            childMeasureHeight = child.getMeasuredHeight();
            Log.i(TAG, "onLayout: getWidth" + getWidth());
            Log.i(TAG, "onLayout: getHeight" + getHeight());
            Log.i(TAG, "onLayout: childMeasureWidth" + childMeasureWidth);
            Log.i(TAG, "onLayout: childMeasureHeight" + childMeasureHeight);
            EasyRelativeLayoutParams lp = (EasyRelativeLayoutParams) child.getLayoutParams();
            switch (lp.position) {
                case EasyRelativeLayoutParams.CENTER:
                    Log.i(TAG, "onLayout: CENTER");
                    l = (getWidth() - childMeasureWidth) / 2;
                    t = (getHeight() - childMeasureHeight) / 2;
                    break;
                case EasyRelativeLayoutParams.LEFT_TOP:
                    Log.i(TAG, "onLayout: LEFT_TOP");
                    l = lp.leftMargin;
                    t = lp.topMargin;
                    break;
                case EasyRelativeLayoutParams.RIGHT_TOP:
                    Log.i(TAG, "onLayout: RIGHT_TOP");
                    l = getWidth() - childMeasureWidth-lp.rightMargin;
                    t = lp.topMargin;
                    break;
                case EasyRelativeLayoutParams.LEFT_BOTTOM:
                    Log.i(TAG, "onLayout: LEFT_BOTTOM");
                    l = lp.leftMargin;
                    t = getHeight() - childMeasureHeight-lp.bottomMargin;
                    break;
                case EasyRelativeLayoutParams.RIGHT_BOTTOM:
                    Log.i(TAG, "onLayout: RIGHT_BOTTOM");
                    l = getWidth() - childMeasureWidth-lp.rightMargin;
                    t = getHeight() - childMeasureHeight-lp.bottomMargin;
                    break;
            }
            child.layout(l, t, l + childMeasureWidth, t + childMeasureHeight);
            Log.i(TAG, "onLayout: l:t:r:b=" + l + ":" + t + ":" + (l + childMeasureWidth) + ":" + (t + childMeasureHeight) + ":");
        }
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new EasyRelativeLayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(LayoutParams p) {
        return p instanceof EasyRelativeLayoutParams;
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new EasyRelativeLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new EasyRelativeLayoutParams(getContext(), attrs);
    }
}
